{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### this demo case requires python 3.9"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5a6ee4684a5c0265"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "initial_id"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "!cd modelscope-agent && git checkout feat/code_interpreter\n",
    "! pip install -r requirements.txt\n",
    "! pip install transformers_stream_generator\n",
    "! pip install invisible-watermark>=2.0"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6a9d565e69358f79"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/demo')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bf28ab696d3c3336"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv  \n",
    "import os\n",
    "from modelscope.utils.config import Config\n",
    "\n",
    "# 配置环境变量，里面需要配置modelscope token来remote调用AI模型\n",
    "os.environ['TOOL_CONFIG_FILE'] ='../config/cfg_tool_template.json'\n",
    "os.environ['OUTPUT_FILE_DIRECTORY'] = '/tmp'\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()\n"
   ],
   "metadata": {
    "collapsed": false,
    "is_executing": true
   },
   "id": "51c03a04400d065c"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from modelscope_agent.llm import LLMFactory\n",
    "\n",
    "model_cfg = {\n",
    "    'qwen_plus':{\n",
    "        'type': 'dashscope',\n",
    "        \"model\": \"qwen-plus\",\n",
    "        \"generate_cfg\": {\n",
    "            \"use_raw_prompt\": True,\n",
    "            \"top_p\": 0.8,\n",
    "            \"top_k\": 10,\n",
    "            \"seed\": 123,\n",
    "            \"debug\": True\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "model_name = 'qwen_plus'\n",
    "llm = LLMFactory.build_llm(model_name, model_cfg)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c0bebd4a47b79fe8"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from modelscope_agent.agent import AgentExecutor\n",
    "from modelscope_agent.tools import CodeInterpreter\n",
    "from  modelscope_agent.agent_types import AgentType\n",
    "\n",
    "# 额外工具列表，这里只有一个code_interpreter\n",
    "additional_tool_list = {\n",
    "    CodeInterpreter.name: CodeInterpreter()\n",
    "}\n",
    "\n",
    "# 构建Agent，需要传入llm，工具配置config以及工具检索\n",
    "agent = AgentExecutor(llm, agent_type=AgentType.MRKL, additional_tool_list=additional_tool_list, tool_retrieval=False)\n",
    "available_tool_list = ['code_interpreter']\n",
    "agent.set_available_tools(available_tool_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5d5e18f0bc25fa39"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 重置对话，清空对话历史\n",
    "agent.reset()\n",
    "\n",
    "# remote=True为调用modelscope api，该服务免费支持QPS较低，建议部署在本地，将remote=False\n",
    "agent.run(\"查看我本地python版本并返回\", remote=False, print_info=True)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6deeccf95f6a118f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
